package com.cuz.hot100;

import com.cuz.daileetcode.utils.ArrayUtils;

public class Q80removeDuplicates {
    public static void main(String[] args) {
//        int[] ints = {0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4};
//        process1(ints);
//        ArrayUtils.print(ints);
        System.out.println(Character.toString((char) ('a' + 1)));

        int[] ints = {1, 1, 1, 2, 2,2,3, 3};
        int i = new Solution().removeDuplicates(ints);
        System.out.println(i);
        ArrayUtils.print(ints);
    }

    static class Solution {


        public synchronized int removeDuplicates(int[] nums) {

            if (nums == null || nums.length <= 2) {
                return nums == null ? 0 : nums.length;
            }
            int effective = 0;
            int curIndex = 1;
            int preNum = nums[0];
            int count = 1;

            //[0,effective)为有效数字
            //[effective,curIndex)为无效数字
            //[curIndex,nums.length) 为未知数字

            while (curIndex < nums.length) {
                int curNum = nums[curIndex];
                if (preNum == curNum) {
                    count++;
                    if (count <= 2) {
                        nums[effective + 1] = curNum;
                    }else {
                        curIndex++;
                        continue;
                    }
                } else {
                    count = 1;
                    preNum = curNum;
                    nums[effective + 1] = curNum;
                }
                effective++;
                curIndex++;
            }
            return effective+1;

        }
    }


    static int process1(int[] nums) {
        if (nums.length < 2) {
            return nums.length;
        }
        int slow = 2;
        int fast = 2;
        int count = 2;
        while (fast < nums.length) {
            if (nums[slow - 2] != nums[fast]) {
                nums[slow] = nums[fast];
                slow++;
                count++;
            }
            fast++;
        }
        return count;
    }

    static int process(int[] nums, int k) {
        int u = 0;
        for (int x : nums) {
            if (u < k || nums[u - k] != x) nums[u++] = x;
        }
        return u;
    }

}
